import { app, BrowserWindow, ipcMain, Menu } from 'electron';
import './src/renderer/store';
const path = require('path');
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */

global.remoteVars = {
	win: null,
	startTime: Date.now(),
	Sno: null,
};
require('./node_extends/HwmSdk');
let resourcesPath = app.getAppPath() + '/resources/';
resourcesPath = resourcesPath.replace(/\\/g, '/');
ipcMain.on('getExePath', (e, args) => {
	e.returnValue = resourcesPath;
});
global.remoteVars.Sno = (function() {
	var sno = 0;
	return function() {
		return sno >= 65535 ? (sno = 1) : ++sno;
	};
})();

if (process.env.NODE_ENV !== 'development') {
	global.__static = require('path')
		.join(__dirname, '/static')
		.replace(/\\/g, '\\\\');
}

let mainWindow;
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html`;
Menu.setApplicationMenu(null);
function createWindow() {
	/**
	 * Initial window options
	 */
	mainWindow = new BrowserWindow({
		height: 563,
		useContentSize: true,
		width: 1000,
	});

	mainWindow.loadURL(winURL);
  global.remoteVars.win = mainWindow;
	mainWindow.on('closed', () => {
		mainWindow = null;
	});
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
	if (process.platform !== 'darwin') {
		app.quit();
	}
});

app.on('activate', () => {
	if (mainWindow === null) {
		createWindow();
	}
});

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
